package com.idealighter.game.web.recharge.service;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.assertions.IdeaAssert;
import com.idealighter.game.player.manager.PlayerMgr;
import com.idealighter.game.server.db.datadb.constant.RechargeRecordStateConstant;
import com.idealighter.game.server.db.datadb.dao.RechargeRecordDao;
import com.idealighter.game.server.db.datadb.generate.domain.RechargeRecordDomain;

import java.util.Map;

@Singleton
public class PayService {
  private RechargeRecordDao rechargeRecordDao;
  @Inject
  private PlayerMgr playerMgr;

  @Inject
  public PayService(RechargeRecordDao rechargeRecordDao) {
    this.rechargeRecordDao = rechargeRecordDao;
  }

  /**
   * 微信支付回调.
   * 
   * @Title weChatPayNotify.
   */
  public boolean weChatPayNotify(Map<String, String> params) {
    String returnCode = params.get("return_code");
    boolean result = false;
    if (returnCode.equals("SUCCESS")) {
      String resultCode = params.get("result_code");
      if (resultCode.equals("SUCCESS")) {
        String orderNo = params.get("out_trade_no");
        // Date payedTime = TimeUtil.parse(params.get("time_end"), "yyyyMMddHHmmss");
        int fen = Integer.parseInt(params.get("total_fee"));
        String outSideOrderNo = params.get("transaction_id");

        RechargeRecordDomain recordDomain =
            rechargeRecordDao.selectRechargeRecordByOrderNo(orderNo);
        if (recordDomain != null) {
          if (recordDomain.getState().equals(RechargeRecordStateConstant.WAIT_PAY)) {
            IdeaAssert.isTrue(fen == recordDomain.getPrice());
            int count = rechargeRecordDao.changeState(recordDomain.getId(),
                RechargeRecordStateConstant.WAIT_PAY, RechargeRecordStateConstant.PAYED);
            IdeaAssert.isTrue(count > 0);

            // 增加用户金币
            boolean pay = playerMgr.recharge(recordDomain.getPlayerId(), recordDomain.getTradeNo(),
                recordDomain.getPayType().toString(), "微信-app支付", recordDomain.getPrice(),
                recordDomain.getGold() + recordDomain.getSendGold(), outSideOrderNo);
            IdeaAssert.isTrue(pay);
          }
          result = true;
        }
      }
    }
    return result;
  }

  /**
   * 支付宝支付回调.
   * 
   * @Title weChatPayNotify.
   */
  public boolean aliPayNotify(Map<String, String> params) {
    String tradeStatus = params.get("trade_status");
    boolean result = false;
    if (tradeStatus.equals("TRADE_SUCCESS")) {
      String orderNo = params.get("out_trade_no");
      int fen = (int) (Float.parseFloat(params.get("total_amount")) * 100);
      String outSideOrderNo = params.get("trade_no");

      RechargeRecordDomain recordDomain = rechargeRecordDao.selectRechargeRecordByOrderNo(orderNo);
      if (recordDomain != null) {
        if (recordDomain.getState().equals(RechargeRecordStateConstant.WAIT_PAY)) {
          IdeaAssert.isTrue(fen == recordDomain.getPrice());
          int count = rechargeRecordDao.changeState(recordDomain.getId(),
              RechargeRecordStateConstant.WAIT_PAY, RechargeRecordStateConstant.PAYED);
          IdeaAssert.isTrue(count > 0);

          // 增加用户金币
          boolean pay = playerMgr.recharge(recordDomain.getPlayerId(), recordDomain.getTradeNo(),
              recordDomain.getPayType().toString(), "微信-app支付", recordDomain.getPrice(),
              recordDomain.getGold() + recordDomain.getSendGold(), outSideOrderNo);
          IdeaAssert.isTrue(pay);
        }
        result = true;
      }
    }
    return result;
  }
}
